//
//  LSRechargeVC.m
//  HappyStore
//
//  Created by 陈锋 on 16/8/11.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "LSRechargeVC.h"
#import "LSPayResultVC.h"

@interface LSRechargeVC (){

    double price;
    NSString * order_no;

}
@property (weak, nonatomic) IBOutlet UITextField *moneyText;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;

@end

@implementation LSRechargeVC

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:YES];
    self.moneyText.text = @"";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView) name:@"alipayResult" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentFailedView) name:@"alipayFailed" object:nil];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"白色背景"] forBarMetrics:UIBarMetricsDefault];
    
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
}
-(void)presentFailedView{
    
    // 跳转至支付失败页面
    LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
    [LSPayResult setCashTypeWithCashFailed];
    [self.navigationController pushViewController:LSPayResult animated:YES];
    
}

-(void)presentView{
    
    [self sendRequest];
    
    //开启侧滑
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title = @"充值";
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:19];
    titleLabel.text = @"充值";
    titleLabel.textColor = RGB(45, 45, 45);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    
    //设置返回按钮
    UIBarButtonItem *leftItem = [UIBarButtonItem itemWithTarget:self action:@selector(leftBarButtonItemClick) image:@"返回" highImage:@"返回"];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    self.payBtn.layer.masksToBounds = YES;
    self.payBtn.layer.cornerRadius = 5.0f;
}
-(void)leftBarButtonItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)clickPayBtn:(UIButton *)sender {
    
    if (self.moneyText.text.length != 0) {
        
            if (self.moneyText) {
                price = [self.moneyText.text floatValue];
            }
        
        order_no = [LSAlipay alipay:@"乐商网" body:@"我的钱包" price:price];

    }else{
    
        NSString *strMsg = @"金额不能为空";
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"支付情况" message:strMsg delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
        [alert show];
    
    }
    
}

-(void)sendRequest{

    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSMutableDictionary *muDicParameters = [NSMutableDictionary dictionary];
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"usertype"] forKey:@"userType"];//用户类型 1-个人，2-企业，3-政府
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
    [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
    [muDicParameters setObject:@(2) forKey:@"payWay"];
    [muDicParameters setObject:@(price) forKey:@"money"];
    [muDicParameters setObject:order_no forKey:@"order_no"];

    
    [LSHttpTool sendHttpData:@"RechargeMoney_app_pay.im.lensum" params:muDicParameters success:^(id responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            self.moneyText.text = @"";
            // 跳转至支付成功页面
            LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
            [LSPayResult setCashTypeWithCashSuccess];
            [self.navigationController pushViewController:LSPayResult animated:YES];
            
        }else{
            
            // 跳转至支付失败页面
            LSPayResultVC * LSPayResult = [[LSPayResultVC alloc]init];
            [LSPayResult setCashTypeWithCashFailed];
            [self.navigationController pushViewController:LSPayResult animated:YES];

            [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];
            
        }

        [self hideBubble];
        
    } failure:^(NSError *error) {

        [self showSingleIconBubble:error.localizedDescription];
        
    }];
    
}
@end
